var fs = require('fs');
var path = require('path');
var express = require('express');
var index = require('../index');
var router = express.Router();

var PostModel = require('../models/posts');
var StudentModel = require('../models/students');
var checkLogin = require('../middlewares/check').checkLogin;

// GET /posts 所有用户或者特定用户的文章页
//   eg: GET /posts?author=xxx
router.get('/', checkLogin, function(req, res, next) {
    var identity = req.session.user.identity;
    var student_id = req.session.user.student_id;

    if ('教师' == identity || '管理员' == identity) {
        StudentModel.getAllStudents()
            .then(function (students) {
                res.render('selectStudents', {
                    students: students
                });
            })
            .catch(function (e) {
                next(e);
            });
    } else {
        PostModel.getPostsByStudentId(student_id)
            .then(function (posts) {
                res.render('gallary', {
                    posts: posts
                });
            })
            .catch(next);
    }
});

// POST 选择学号后生成相册
router.post('/', function (req, res, next) {
    var student_id = req.fields.student_id;
    PostModel.getPostsByStudentId(student_id)
        .then(function (posts) {
            res.render('gallary', {
                posts: posts
            });
        })
        .catch(next);
});

module.exports = router;
